<template>
	<div>
		<div style="padding: 1vw;">
			<van-nav-bar title="发布帖子" right-text="发布" @click-right="publishBlog">
			</van-nav-bar>
			<div style="margin-top: 2vh;">
				<el-input v-model="blogText" :rows="10" type="textarea" :placeholder="textAreaPlaceHolder" />
			</div>
			<div style="margin-top: 2vh;">
				<van-uploader v-model="imgList" :max-count="9" multiple />
			</div>
		</div>

	</div>
</template>

<script>
	import {
		showNotify
	} from 'vant';
	import url from '@/assets/url.json';
	
	export default {
		props:{
			close:Function
		},
		
		mounted() {
			this.textAreaPlaceHolder=this.placeHolderList[Math.floor(Math.random() * this.placeHolderList.length)];
		},
		
		data() {
			return {
				blogText: '',
				placeHolderList: [
					"落霞与孤鹜齐飞，秋水共长天一色", 
					"云销雨霁，彩彻区明", 
					"鹤汀凫渚，穷岛屿之萦回", 
					"江畔何人初见月？ 江月何年初照人？", 
					"一日昼寝帐中，落被于地",
					"人生代代无穷已，江月年年望相似",
					"良言一句三冬暖，恶语伤人六月寒"
				],
				textAreaPlaceHolder:'',
				imgList: []
			}
		},
		methods: {
			publishBlog() {
				
				let imageList=[];
				for(let key in this.imgList){
					imageList.push(this.imgList[key].file);
				}
				let formData = new FormData();
				
				formData.append('context',this.blogText);
				for(let key in imageList){
					formData.append('images',imageList[key]);
				}
				
				this.$axios({
					url:url.ip+'/api-blog'+'/blog/publish',
					method:'POST',
					data:formData,
					headers:{
						token: this.$cookies.get('token'),
						'Content-Type': 'multipart/form-data',
					}
				}).then((res)=>{
					if(res.data.code!==0){
						console.log(res);
						showNotify({ type: 'danger', message: res.data.message });
					}else{
						showNotify({ type: 'success', message: '发布成功' });
						this.close();
					}
				}).catch((err)=>{
					showNotify({ type: 'danger', message: err });
				})
			}
		}
	}
</script>

<style>
</style>